package com.examination.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.examination.client.domian.dto.QueryOrderDTO;
import com.examination.client.domian.dto.QueryOrderRecordDTO;
import com.examination.client.domian.vo.OrderRecordVO;
import com.examination.client.domian.vo.QueryOrderRecordVO;
import com.examination.client.entity.BuyMessageRecord;
import com.examination.client.entity.CourierSite;
import com.examination.client.entity.UserInfo;
import com.examination.client.enums.OrderStatusEnum;
import com.examination.client.mapper.BuyMessageRecordMapper;
import com.examination.client.mapper.CourierSiteMapper;
import com.examination.client.service.IBuyMessageRecordService;
import com.examination.client.service.IUserInfoService;
import com.examination.client.service.OrderService;
import com.examination.client.util.UserInfoUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<BuyMessageRecordMapper, BuyMessageRecord> implements OrderService {
    @Resource
    IBuyMessageRecordService buyMessageRecordService;
    @Resource
    IUserInfoService userInfoService;


    @Override
    public Page<OrderRecordVO> list(QueryOrderDTO queryOrderDTO) {
        LambdaQueryWrapper<BuyMessageRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BuyMessageRecord::getOrderStatus, OrderStatusEnum.ALREADY_PAY.getCode());
        queryWrapper.eq(BuyMessageRecord::getSiteId, UserInfoUtil.getSiteId());
        queryWrapper.orderByDesc(BuyMessageRecord::getUpdateTime);
        Page<BuyMessageRecord> QueryPage = new Page<>();
        QueryPage.setCurrent(queryOrderDTO.getCurrent());
        QueryPage.setSize(queryOrderDTO.getSize());
        Page<BuyMessageRecord> pageList = buyMessageRecordService.page(QueryPage, queryWrapper);
        Page page = new Page<>();
        BeanUtil.copyProperties(pageList,page);
        if(Objects.nonNull(pageList) && CollectionUtil.isNotEmpty(pageList.getRecords())){
            List<OrderRecordVO> orderRecordVOList = Lists.newArrayList();
            pageList.getRecords().forEach(e->{
                OrderRecordVO orderRecordVO = new OrderRecordVO();
                orderRecordVO.setOrderAmount(e.getOrderAmount());
                orderRecordVO.setUpdateTime(e.getUpdateTime());
                orderRecordVO.setMessageNum(e.getMessageNum());
                orderRecordVOList.add(orderRecordVO);
            });
            page.setRecords(orderRecordVOList);
        }
        return page;
    }

    @Override
    public Page<QueryOrderRecordVO> record(QueryOrderRecordDTO queryOrderRecordDTO) {
        LambdaQueryWrapper<BuyMessageRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BuyMessageRecord::getOrderStatus, OrderStatusEnum.ALREADY_PAY.getCode());
        queryWrapper.ge(StringUtils.isNotBlank(queryOrderRecordDTO.getStartTime()),BuyMessageRecord::getCreateTime, queryOrderRecordDTO.getStartTime());
        queryWrapper.le(StringUtils.isNotBlank(queryOrderRecordDTO.getStartTime()),BuyMessageRecord::getCreateTime, queryOrderRecordDTO.getEndTime());
        if(Objects.nonNull(queryOrderRecordDTO.getSiteId())){
            queryWrapper.eq(BuyMessageRecord::getSiteId, queryOrderRecordDTO.getSiteId());
        }
        queryWrapper.orderByDesc(BuyMessageRecord::getUpdateTime);
        Page<BuyMessageRecord> QueryPage = new Page<>();
        QueryPage.setCurrent(queryOrderRecordDTO.getCurrent());
        QueryPage.setSize(queryOrderRecordDTO.getSize());
        Page<BuyMessageRecord> pageList = buyMessageRecordService.page(QueryPage, queryWrapper);
        Page page = new Page<>();
        BeanUtil.copyProperties(pageList,page);
        if(Objects.nonNull(pageList) && CollectionUtil.isNotEmpty(pageList.getRecords())){
            List<QueryOrderRecordVO> orderRecordVOList = Lists.newArrayList();
            pageList.getRecords().forEach(e->{
                log.info("e.getOperatorId() --> " + e.getOperatorId());
                UserInfo userInfo = userInfoService.getById(e.getOperatorId());
                if (userInfo != null) {
                    QueryOrderRecordVO orderRecordVO = new QueryOrderRecordVO();
                    orderRecordVO.setUserName(userInfo.getUserName());
                    orderRecordVO.setNickName(userInfo.getNickName());
                    orderRecordVO.setUserPhone(userInfo.getUserPhone());
                    orderRecordVO.setMessageNum(e.getMessageNum());
                    orderRecordVO.setCreateTime(e.getCreateTime());
                    orderRecordVO.setId(e.getId());
                    orderRecordVOList.add(orderRecordVO);
                }
            });
            page.setRecords(orderRecordVOList);
        }
        return page;
    }
}
